package com.jk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jk.mapper.ZSLMapper;
import com.jk.model.*;
import com.jk.service.ZSLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class ZSLServiceImpl implements ZSLService {

    @Autowired
    private ZSLMapper zslMapper;


    /**
     * 查询树
     * @return
     */
    @Override
    public List<ManageBean> findManageTree() {
        int id=0;
        List<ManageBean> list=findManage(id);
        return list;
    }

    /**
     * 查询用户信息
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @Override
    public Map findUserList(Integer page, Integer rows, UserBean userBean) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("userBean", userBean);
        int count=zslMapper.findUserCount(param);
        result.put("total", count);
        param.put("startIndex", (page-1)*rows);
        param.put("endIndex", rows);
        List<UserBean> list=zslMapper.findUserList(param);
        result.put("rows", list);
        return result;
    }

    /**
     * 删除用户信息
     * @param ids
     */
    @Override
    public void delUserById(Integer[] ids) {
        zslMapper.delUserById(ids);
    }

    /**
     * 查询店铺信息
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @Override
    public Map findUserShopList(Integer page, Integer rows, UserBean userBean) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("userBean", userBean);
        int count=zslMapper.findUserShopCount(param);
        result.put("total", count);
        param.put("startIndex", (page-1)*rows);
        param.put("endIndex", rows);
        List<UserBean> list=zslMapper.findUserShopList(param);
        result.put("rows", list);
        return result;
    }

    /**
     * 查询店铺所拥有的山商品
     * @param page
     * @param rows
     * @param productBean
     * @param id
     * @return
     */
    @Override
    public Map shopTable(Integer page, Integer rows, ProductBean productBean, Integer id) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("productBean", productBean);
        int count=zslMapper.shopTableCount(param);
        result.put("total", count);
        param.put("startIndex", (page-1)*rows);
        param.put("endIndex", rows);
        param.put("stockId", id);
        List<ProductBean> list=zslMapper.shopTableList(param);
        result.put("rows", list);
        return result;
    }

    /**
     * 查询所有订单信息
     * @param page
     * @param rows
     * @param orderBean
     * @return
     */
    @Override
    public Map findOrderList(Integer page, Integer rows, OrderBean orderBean) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("orderBean", orderBean);
        int count=zslMapper.findOrderCount(param);
        result.put("total", count);
        param.put("startIndex", (page-1)*rows);
        param.put("endIndex", rows);
        List<OrderBean> list=zslMapper.findOrderList(param);
        result.put("rows", list);
        return result;
    }

    /**
     * 查询所有申请的店铺
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @Override
    public Map findShopUserList(Integer page, Integer rows, UserBean userBean) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("userBean", userBean);
        int count=zslMapper.findShopUserCount(param);
        result.put("total", count);
        param.put("startIndex", (page-1)*rows);
        param.put("endIndex", rows);
        List<UserBean> list=zslMapper.findShopUserList(param);
        result.put("rows", list);
        return result;
    }

    /**
     * 根据商品id查询订单
     * @param page
     * @param rows
     * @param orderBean
     * @param id
     * @return
     */
    @Override
    public Map orderTable(Integer page, Integer rows, OrderBean orderBean, Integer id) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("orderBean", orderBean);
        param.put("id", id);
        int count=zslMapper.orderTableCount(param);
        result.put("total", count);
        param.put("startIndex", (page-1)*rows);
        param.put("endIndex", rows);
        List<OrderBean> list=zslMapper.orderTableList(param);
        result.put("rows", list);
        return result;
    }

    /**
     * 购物车查询数据
     * @param productId
     * @return
     */
    @Override
    public ProductBean queryProductByProductId(Integer productId) {
        return zslMapper.queryProductByProductId(productId);
    }

    @Override
    public List<AcceptsiteBean> chaAddress(int i) {
        return zslMapper.chaAddress(i);
    }

    /**
     * 根据订单查询商品信息
     * @param productId
     * @return
     */
    @Override
    public OrderBean chajiaoyi(Integer productId) {
        return zslMapper.chajiaoyi(productId);
    }

    /**
     * 递归树
     * @param id
     * @return
     */
    private List<ManageBean> findManage(int id) {
        List<ManageBean> list= zslMapper.findManage(id);
        for (ManageBean manageBean : list) {
            Integer id1 = manageBean.getId();
            List<ManageBean> manage = findManage(id1);
            if (manage != null && manage.size() >0){
                manageBean.setNodes(manage);
                manageBean.setSelectable(false);
            }else {
                manageBean.setSelectable(true);
            }
        }
        return list;
    }

}
